install.packages(c("tidyverse", "tidycensus", "tm", "openintro",
                   "scales", "maps", "mapproj"))
library(tidyverse)
library(tidycensus)
library(tm)
library(openintro)
library(scales)
library(maps)
library(mapproj)

## FIGURE 1 ##
county_map = map_data("county")

data(fips_codes)
fips_codes = fips_codes %>%
  mutate(fips = paste(state_code, county_code, sep = ""),
         fips = str_remove(fips, "^0+"),
         county = removeWords(county, "County"),
         county = tolower(county),
         county = str_trim(county),
         state = tolower(state_name)) %>%
  select(state, county, fips)

county_map = county_map %>%
  left_join(., fips_codes, by = c("region" = "state",
                                  "subregion" = "county"))

## Get list of missing county FIPS
# missing_fips = county_map %>%
#   filter(is.na(fips)) %>%
#   distinct(region, subregion)

## Filling in missing FIPS codes
county_map = county_map %>%
  mutate(fips = case_when(region == "alabama" &
                            subregion == "de kalb" ~ "1049",
                          region == "alabama" &
                            subregion == "st clair" ~ "1115",
                          region == "arkansas" & 
                            subregion == "st francis" ~ "5123",
                          region == "district of columbia" &
                            subregion == "washington" ~ "11001",
                          region == "florida" &
                            subregion == "de soto" ~ "12027",
                          region == "florida" & 
                            subregion == "st johns" ~ "12109",
                          region == "florida" &
                            subregion == "st lucie" ~ "12111",
                          region == "georgia" &
                            subregion == "de kalb" ~ "13089",
                          region == "illinois" &
                            subregion == "de kalb" ~ "17037",
                          region == "illinois" &
                            subregion == "du page" ~ "17043",
                          region == "illinois" &
                            subregion == "la salle" ~ "17099",
                          region == "illinois" &
                            subregion == "st clair" ~ "17163",
                          region == "indiana" &
                            subregion == "de kalb" ~ "18033",
                          region == "indiana" &
                            subregion == "la porte" ~ "18091",
                          region == "indiana" & 
                            subregion == "st joseph" ~ "18141",
                          region == "iowa" &
                            subregion == "obrien" ~ "19141",
                          region == "louisiana" &
                            subregion ==  "acadia" ~ "22001",
                          region == "louisiana" &
                            subregion ==  "allen" ~ "22003",
                          region == "louisiana" &
                            subregion ==  "ascension" ~ "22005",
                          region == "louisiana" &
                            subregion ==  "assumption" ~ "22007",
                          region == "louisiana" &
                            subregion ==  "avoyelles" ~ "22009",
                          region == "louisiana" &
                            subregion ==  "beauregard" ~ "22011",
                          region == "louisiana" &
                            subregion ==  "bienville" ~ "22013",
                          region == "louisiana" &
                            subregion ==  "bossier" ~ "22015",
                          region == "louisiana" &
                            subregion ==  "caddo" ~ "22017",
                          region == "louisiana" &
                            subregion ==  "calcasieu" ~ "22019",
                          region == "louisiana" &
                            subregion ==  "caldwell" ~ "22021",
                          region == "louisiana" &
                            subregion ==  "cameron" ~ "22023",
                          region == "louisiana" &
                            subregion ==  "catahoula" ~ "22025",
                          region == "louisiana" &
                            subregion ==  "claiborne" ~ "22027",
                          region == "louisiana" &
                            subregion ==  "concordia" ~ "22029",
                          region == "louisiana" &
                            subregion ==  "de soto" ~ "22031",
                          region == "louisiana" &
                            subregion ==  "east baton rouge" ~ "22033",
                          region == "louisiana" &
                            subregion ==  "east carroll" ~ "22035",
                          region == "louisiana" &
                            subregion ==  "east feliciana" ~ "22037",
                          region == "louisiana" &
                            subregion ==  "evangeline" ~ "22039",
                          region == "louisiana" &
                            subregion ==  "franklin" ~ "22041",
                          region == "louisiana" &
                            subregion ==  "grant" ~ "22043",
                          region == "louisiana" &
                            subregion ==  "iberia" ~ "22045",
                          region == "louisiana" &
                            subregion ==  "iberville" ~ "22047",
                          region == "louisiana" &
                            subregion ==  "jackson" ~ "22049",
                          region == "louisiana" &
                            subregion ==  "jefferson" ~ "22051",
                          region == "louisiana" &
                            subregion ==  "jefferson davis" ~ "22053",
                          region == "louisiana" &
                            subregion ==  "lafayette" ~ "22055",
                          region == "louisiana" &
                            subregion ==  "lafourche" ~ "22057",
                          region == "louisiana" &
                            subregion ==  "la salle" ~ "22059",
                          region == "louisiana" &
                            subregion ==  "lincoln" ~ "22061",
                          region == "louisiana" &
                            subregion ==  "livingston" ~ "22063",
                          region == "louisiana" &
                            subregion ==  "madison" ~ "22065",
                          region == "louisiana" &
                            subregion ==  "morehouse" ~ "22067",
                          region == "louisiana" &
                            subregion ==  "natchitoches" ~ "22069",
                          region == "louisiana" &
                            subregion ==  "orleans" ~ "22071",
                          region == "louisiana" &
                            subregion ==  "ouachita" ~ "22073",
                          region == "louisiana" &
                            subregion ==  "plaquemines" ~ "22075",
                          region == "louisiana" &
                            subregion ==  "pointe coupee" ~ "22077",
                          region == "louisiana" &
                            subregion ==  "rapides" ~ "22079",
                          region == "louisiana" &
                            subregion ==  "red river" ~ "22081",
                          region == "louisiana" &
                            subregion ==  "richland" ~ "22083",
                          region == "louisiana" &
                            subregion ==  "sabine" ~ "22085",
                          region == "louisiana" &
                            subregion ==  "st bernard" ~ "22087",
                          region == "louisiana" &
                            subregion ==  "st charles" ~ "22089",
                          region == "louisiana" &
                            subregion ==  "st helena" ~ "22091",
                          region == "louisiana" &
                            subregion ==  "st james" ~ "22093",
                          region == "louisiana" &
                            subregion ==  "st john the baptist" ~ "22095",
                          region == "louisiana" &
                            subregion ==  "st landry" ~ "22097",
                          region == "louisiana" &
                            subregion ==  "st martin" ~ "22099",
                          region == "louisiana" &
                            subregion ==  "st mary" ~ "22101",
                          region == "louisiana" &
                            subregion ==  "st tammany" ~ "22103",
                          region == "louisiana" &
                            subregion ==  "tangipahoa" ~ "22105",
                          region == "louisiana" &
                            subregion ==  "tensas" ~ "22107",
                          region == "louisiana" &
                            subregion ==  "terrebonne" ~ "22109",
                          region == "louisiana" &
                            subregion ==  "union" ~ "22111",
                          region == "louisiana" &
                            subregion ==  "vermilion" ~ "22113",
                          region == "louisiana" &
                            subregion ==  "vernon" ~ "22115",
                          region == "louisiana" &
                            subregion ==  "washington" ~ "22117",
                          region == "louisiana" &
                            subregion ==  "webster" ~ "22119",
                          region == "louisiana" &
                            subregion ==  "west baton rouge" ~ "22121",
                          region == "louisiana" &
                            subregion ==  "west carroll" ~ "22123",
                          region == "louisiana" &
                            subregion ==  "west feliciana" ~ "22125",
                          region == "louisiana" &
                            subregion ==  "winn" ~ "22127",
                          region == "maryland" &
                            subregion == "prince georges" ~ "24033",
                          region == "maryland" &
                            subregion == "queen annes" ~ "24035",
                          region == "maryland" &
                            subregion == "st marys" ~ "24037",
                          region == "michigan" &
                            subregion == "st clair" ~ "26147",
                          region == "michigan" &
                            subregion == "st joseph" ~ "26149",
                          region == "minnesota" &
                            subregion == "st louis" ~ "27137",
                          region == "mississippi" &
                            subregion == "de soto" ~ "28033",
                          region == "missouri" &
                            subregion == "de kalb" ~ "29063",
                          region == "missouri" & 
                            subregion == "st charles" ~ "29183",
                          region == "missouri" &
                            subregion == "st clair" ~ "29185",
                          region == "missouri" &
                            subregion == "st francois" ~ "29187",
                          region == "missouri" &
                            subregion == "st louis" ~ "29189",
                          region == "missouri" &
                            subregion == "st louis city" ~ "29510",
                          region == "missouri" &
                            subregion == "ste genevieve" ~ "29186",
                          region == "montana" &
                            subregion == "yellowstone national" ~ "30113",
                          region == "new york" &
                            subregion == "st lawrence" ~ "36089",
                          region == "north dakota" &
                            subregion == "la moure" ~ "38045",
                          region == "tennessee" &
                            subregion == "de kalb" ~ "47041",
                          region == "texas" &
                            subregion == "de witt" ~ "48123",
                          region == "virginia" &
                            subregion == "suffolk" ~ "51800",
                          region == "virginia" &
                            subregion == "hampton" ~ "51650",
                          region == "virginia" &
                            subregion == "newport news" ~ "51700",
                          region == "virginia" &
                            subregion == "norfolk" ~ "51710",
                          region == "virginia" &
                            subregion == "virginia beach" ~ "51810",
                          region == "wisconsin" &
                            subregion == "st croix" ~ "55109",
                          TRUE ~ fips))

## Merge in my new county FIPS codes
county_map = county_map %>%
  mutate(fips = case_when(fips %in% c("12025") ~ "12086",
                          fips %in% c("36005", "36047", "36061", "36081", "36085") ~ "36500",
                          fips %in% c("13041", "13121", "13203") ~ "13500",
                          fips %in% c("29189", "29510") ~ "29900",
                          fips %in% c("51003", "51540") ~ "51900",
                          fips %in% c("51005", "51560") ~ "51902",
                          fips %in% c("51013", "51510") ~ "51904",
                          fips %in% c("51015", "51790") ~ "51906",
                          fips %in% c("51031", "51680") ~ "51908",
                          fips %in% c("51053", "51730") ~ "51910",
                          fips %in% c("51055", "51650") ~ "51912",
                          fips %in% c("51069", "51840") ~ "51914",
                          fips %in% c("51087", "51760") ~ "51916",
                          fips %in% c("51089", "51690") ~ "51918",
                          fips %in% c("51095", "51830") ~ "51920",
                          fips %in% c("51121", "51750") ~ "51922",
                          fips %in% c("51123", "51800") ~ "51924",
                          fips %in% c("51129", "51710", "51785", "51740") ~ "51926",
                          fips %in% c("51143", "51590") ~ "51928",
                          fips %in% c("51149", "51670") ~ "51930",
                          fips %in% c("51161", "51770") ~ "51932",
                          fips %in% c("51163", "51530") ~ "51934",
                          fips %in% c("51165", "51660") ~ "51936",
                          fips %in% c("51177", "51630") ~ "51938",
                          fips %in% c("51189", "51700") ~ "51940",
                          fips %in% c("51191", "51520") ~ "51942",
                          TRUE ~ fips),
         fips = as.numeric(fips))

## Loading New Deal spending data
load("ndeal.RData")

## Merge county map and New Deal data
ndeal_map = county_map %>%
  left_join(., ndeal, by = c("fips" = "my_fips")) %>%
  mutate(wpa = ntile(pc_wpa, 5), ## Create spending quintiles 
         pwa = ntile(pc_pwa, 5))

## Panel A
wpa_map = ggplot(ndeal_map %>% filter(!is.na(wpa)), 
                 aes(x = long, y = lat, 
                     group = group, fill = factor(wpa))) + 
  geom_polygon(size = 0.10) +
  coord_map() +
  scale_fill_brewer(palette = "YlGnBu", labels = c("Q1", "Q2", "Q3", "Q4",
                                                   "Q5")) + 
  labs(y = NULL, x = NULL,
       fill = NULL) +
  theme_classic() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        axis.line.x = element_blank(),
        axis.line.y = element_blank(),
        legend.position = c(0.2, 0.1)) +
  guides(fill = guide_legend(nrow = 1))

## Panel B
pwa_map = ggplot(ndeal_map %>% filter(!is.na(pwa)), 
                 aes(x = long, y = lat, 
                     group = group, fill = factor(pwa))) + 
  geom_polygon(size = 0.10) +
  coord_map() +
  scale_fill_brewer(palette = "YlGnBu", labels = c("Q1", "Q2", "Q3", "Q4",
                                                   "Q5")) + 
  labs(y = NULL, x = NULL,
       fill = NULL) +
  theme_classic() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        axis.line.x = element_blank(),
        axis.line.y = element_blank(),
        legend.position = c(0.2, 0.1)) +
  guides(fill = guide_legend(nrow = 1))


